package animationEngine;

import java.awt.Component;
import java.awt.Image;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

/**
 * The Class AnimationControlsFrame, this class is the frame that holds all of the buttons
 * (and the speed slider) that allow the user to manipulating the animation.
 * 
 * @author Andrei Palade
 * @author David McBrierty
 * @author Garry Sharp
 * @author Hristo Georgiev
 * @author John Dennis
 * @author Marco Sarconi
 * @version 1.0.1
 */
public class AnimationControlsFrame extends JInternalFrame {
	
	/** Auto generated serial version ID */
	private static final long serialVersionUID = -6999549423510780499L;
	/** Static variables for the speed slider bar */
	public static final int SLOWEST_SPEED = 1;	//minimum value
	public static final int FASTEST_SPEED = 10;	//maximum value
	public static final int START_SPEED = 5;	//starting value
	
	/** The buttons to display in this frame. */
	public Image playButtonIcon;
	public JButton playButton;
	public Image pauseButtonIcon;
	public JButton pauseButton;
	public Image forwardButtonIcon;
	public JButton forwardButton;
	public Image backwardButtonIcon;
	public JButton backwardButton;
	public Image resetButtonIcon;
	public JButton resetButton;
	public Image swapViewsButtonIcon;
	public JButton swapViewsButton;
	
	public JPanel buttonPanel;	//A JPanel to hold all the buttons
	/** The speed slider bar and its label */
	public JLabel sliderLabel;
	public JSlider speedSlider;
	
	/**
	 * Constructor.  Takes a MainFrame parameter so that the listeners can be set up 
	 * for the buttons and the speed slider.
	 * 
	 * @param mainFrame The MainFrame that this frame has been created by.
	 * @throws InterruptedException 
	 */
	public AnimationControlsFrame(MainFrame mainFrame) throws InterruptedException{
		super("Animation Controls", true, false, false, true);
		
		//Initialise each button and set its actionlistener to the given mainFrame
		//Play Button
		try{
			this.playButtonIcon = ImageIO.read(ClassLoader.getSystemResourceAsStream("images/Play_24x24.png"));
		}catch(Exception e){
			//System.out.println("Image loaded through Eclipse");
		}
		try{
			this.playButtonIcon = ImageIO.read(new File("images/Play_24x24.png"));
		}catch(Exception e){
			//System.out.println("Image loaded through a jar file");
		}
	
		this.playButton = new JButton();
		this.playButton.setIcon(new ImageIcon(this.playButtonIcon));
		//this.playButton.setDisabledIcon(new ImageIcon("images/" +
				//"Play_48x48_Disabled.png"));
	    //this.playButton.setEnabled(false);
		this.playButton.addActionListener(mainFrame);
		this.playButton.setToolTipText("Play animation from current position");
		//System.out.println("Added listener of " + this.getParent());
		
		
		//Pause Button
		try{
			this.pauseButtonIcon = ImageIO.read(ClassLoader.getSystemResourceAsStream("images/Pause_24x24.png"));
		}catch(Exception e){
			//System.out.println("Image loaded through Eclipse");
		}
		try{
			this.pauseButtonIcon = ImageIO.read(new File("images/Pause_24x24.png"));
		}catch(Exception e){
			//System.out.println("Image loaded through a jar file");
		}
		this.pauseButton = new JButton();
		this.pauseButton.setIcon(new ImageIcon(this.pauseButtonIcon));
		this.pauseButton.addActionListener(mainFrame);
		this.pauseButton.setToolTipText("Freeze the animation at its current position");
		
		
		//Forward Button
		try{
			this.forwardButtonIcon = ImageIO.read(ClassLoader.getSystemResourceAsStream("images/Next.png"));
		}catch(Exception e){
			//System.out.println("Image loaded through Eclipse");
		}
		try{
			this.forwardButtonIcon = ImageIO.read(new File("images/Next.png"));
		}catch(Exception e){
			//System.out.println("Image loaded through a jar file");
		}
		this.forwardButton = new JButton();
		this.forwardButton.setIcon(new ImageIcon(this.forwardButtonIcon));
		this.forwardButton.addActionListener(mainFrame);
		this.forwardButton.setToolTipText("Go forwards one step");
	
		
		//Backward Button
		try{
			this.backwardButtonIcon = ImageIO.read(ClassLoader.getSystemResourceAsStream("images/Back.png"));
		}catch(Exception e){
			//System.out.println("Image loaded through Eclipse");
		}
		try{
			this.backwardButtonIcon = ImageIO.read(new File("images/Back.png"));
		}catch(Exception e){
			//System.out.println("Image loaded through a jar file");
		}
		this.backwardButton = new JButton();
		this.backwardButton.setIcon(new ImageIcon(this.backwardButtonIcon));
		this.backwardButton.addActionListener(mainFrame);
		this.backwardButton.setToolTipText("Go backwards one step");


		//Reset Button
		//Backward Button
		try{
			this.resetButtonIcon = ImageIO.read(ClassLoader.getSystemResourceAsStream("images/Refresh_24x24.png"));
		}catch(Exception e){
			//System.out.println("Image loaded through Eclipse");
		}
		try{
			this.resetButtonIcon = ImageIO.read(new File("images/Refresh_24x24.png"));
		}catch(Exception e){
			//System.out.println("Image loaded through a jar file");
		}
		this.resetButton = new JButton();
		this.resetButton.setIcon(new ImageIcon(this.resetButtonIcon));
		this.resetButton.addActionListener(mainFrame);
		this.resetButton.setToolTipText("Reset animation back to beginning");

		
		try{
			this.swapViewsButtonIcon = ImageIO.read(ClassLoader.getSystemResourceAsStream("images/flip.png"));
		}catch(Exception e){
			//System.out.println("Image loaded through Eclipse");
		}
		try{
			this.swapViewsButtonIcon = ImageIO.read(new File("images/flip.png"));
		}catch(Exception e){
			//System.out.println("Image loaded through a jar file");
		}
		this.swapViewsButton = new JButton();
		this.swapViewsButton.setIcon(new ImageIcon(this.swapViewsButtonIcon));
		this.swapViewsButton.addActionListener(mainFrame);
		this.swapViewsButton.setToolTipText("Swap the section and full animation displays");
		
		//Create a JPanel to hold all of the buttons we wish to display
		buttonPanel = new JPanel();
		//Set the panel to have a horizontal layout
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		//Add all the buttons to our button panel
		
		buttonPanel.add(this.backwardButton);
		buttonPanel.add(this.playButton);
		buttonPanel.add(this.pauseButton);
		buttonPanel.add(this.forwardButton);
		buttonPanel.add(this.resetButton);		
		buttonPanel.add(this.swapViewsButton);
		
		//Create the sped slider label.
        sliderLabel = new JLabel("Animation Speed", JLabel.CENTER);
        sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		//Set up the speed slider
		this.speedSlider = new JSlider(JSlider.HORIZONTAL, SLOWEST_SPEED, FASTEST_SPEED, START_SPEED);
		speedSlider.setName("Animation Speed");
		//Turn on labels at major tick marks.
		speedSlider.setMajorTickSpacing(1);
		speedSlider.setToolTipText("Controls the speed of the animation");
		//speedSlider.setMinorTickSpacing(1);
		speedSlider.setPaintTicks(true);
		speedSlider.setPaintLabels(true);
		speedSlider.setSize(380, 30);
		//Add the listener for this slider to the main frame of our application (given param)
		speedSlider.addChangeListener(mainFrame);
		
		//Set the layout for this frame
		this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
		//Add our button panel, slider label and slider bar.
		this.add(buttonPanel);
		this.add(sliderLabel);
		this.add(speedSlider);
		
		//Set out location and size
		this.setLocation(350, 0);
	    this.setSize(500, 150);
	    this.setVisible(true);		
	}
	

}
